<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* 	Modèle pour la table Region
**/


class Region extends CI_Model
{
	/*
	* Attributs statiques de la classe Region
	*/
	public static $table = 'old_region';
	
	/**
	* Attribut d'une instance Region
	*/
	
	public $key;
	public $name;
	public $desc;
	public $svg;
	
	
	/**
	* Fonction permettant la recherche d'une region par sa clé (verification d'existance incluse)
	* @param cle d'une region
	*/
	public static function getRegionByKey($key = '')
	{
		$region = null;
		
			if(Region::isRegionExist($key))
			{
				$region = new Region();
				
				$sql = 'SELECT * FROM '.Region::$table.' WHERE cle_region = ?';
				
				$CI =& get_instance();	
				$query = $CI->db->query($sql,array($key));
				$row = $query->row();
				
				$region->key = $key;
				$region->name = $row->nom_region;
				$region->desc = $row->desc_region;
				$region->svg = $row->sgv_region;

				$query->free_result();
			}
			
		return $region;
	}
	
	/**
	* Fonction determinant si la region X existe
	* @param identifia
	*/
	public static function isRegionExist($key = '')
	{
		$res = false;
		$sql = 'SELECT * FROM '.Region::$table.' WHERE cle_region = ?';
		
		$CI =& get_instance();
		$query = $CI->db->query($sql,array($key));
		if($query->num_rows() > 0)
		{
				$res = true;
		}
			
		$query->free_result();

		return $res;
	}

	/**
	* Fonction permettant de retourner toutes les régions
	*/
	public static function getAllRegions()
	{
		$list_regions = array();
		$sql = 'SELECT * FROM '.Region::$table.'';
				
		$CI =& get_instance();	
		$query = $CI->db->query($sql);
				
			foreach ($query->result() as $row)
			{
				$region = new Region();

				$region->key = $row->cle_region;
				$region->name = $row->nom_region;
				$region->desc = $row->desc_region;
				$region->svg = $row->sgv_region;
				
				$list_regions[] = $region;
			}

		$query->free_result();
		
		return $list_regions;
	}
	
	/**
	* Fonction permettant de retourner toutes les régions
	*/
	public static function getAllRegionsJSON()
	{
		$sql = 'SELECT * FROM '.Region::$table.'';
				
		$CI =& get_instance();	
		$query = $CI->db->query($sql);
				
		return $query->result();
	}
	
	/**
	* Fonction comptant le nombre d'utilisateurs totals dans la base
	*/
	public static function countRegions()
	{
			$sql = 'SELECT * FROM '.Region::$table;
			$CI =& get_instance();
			$query = $CI->db->query($sql);
			
			return $query->num_rows();
	}
	
	/**
	* Fonction permettant d'afficher les informations de l'utilisateur (mode debug)
	*/
	public function toString()
	{
		echo 'ID=' . $this->id . ' --- ';
		echo 'NOM=' . $this->name . ' --- ';
		echo 'PRENOM=' . $this->vorname  . ' --- ';
		echo 'EMAIL=' . $this->email . ' --- ';
		echo 'SEXE =' . $this->sexe . ' --- ';
		echo 'DATE=' . $this->date . ' --- ' ;
		echo 'PASSWORD=' . $this->password . ' --- ';

	}
	
}

?>
